package day23;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


/**
 *
 * TextUploadServer.java
 * @author wangbiao<br/>
 *         email:785497344@qq.com
 * @description 文本上传的服务端
 * @since 1.0.0
 * @date 2013-7-11下午11:57:16
 *
 */
public class TextUploadServer {

	public static void main(String[] args) {
		ServerSocket ss = null;
		Socket s = null;
		PrintWriter writer = null;
		try {
			ss = new ServerSocket(10000);
			s = ss.accept();
			
			String ip = s.getInetAddress().getHostAddress();
			System.out.println(ip+"connect !");
			
			BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
			writer = new PrintWriter(new FileWriter("server.txt"), true);
			String line = null;
			while ((line=in.readLine()) != null) {
				writer.println(line);
			}
			
			PrintWriter out = new PrintWriter(s.getOutputStream(), true);
			out.println("上传成功！OK");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != writer) {
				writer.close();
			}
			if (null != s) {
				try {
					s.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != ss) {
				try {
					ss.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}
